package com.lili.operate.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lili.model.operate.SearchHotWords;
import com.lili.operate.mapper.HotWordsMapper;
import com.lili.operate.service.HotWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class HotWordsServiceImpl implements HotWordsService {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    HotWordsMapper hotWordsMapper;
    @Override
    public int saveHotWords(SearchHotWords searchHotWords) {
        return hotWordsMapper.insert(searchHotWords);
    }

    /**
     * 查看某一天的历史热词
     * @param time
     * @return
     */
    @Override
    public List<SearchHotWords> queryHistoryWords(String time) {
        //创建时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime thisDay = LocalDateTime.parse(time, formatter);
        //获取后一天时间
        String nextDay = thisDay.plusDays(1).format(formatter);
        QueryWrapper<SearchHotWords> wrapper = new QueryWrapper<>();
        wrapper.between("create_time",thisDay,nextDay).eq("isdel",0);
        List<SearchHotWords> list = hotWordsMapper.selectList(wrapper);
        return list;
    }

    /**
     * 查询近七天搜索热度最高的
     * @param time
     * @return
     */
    @Override
    public List<SearchHotWords> queryRecentlyWords(String time) {
       DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
       LocalDateTime thisDay = LocalDateTime.parse(time,dateTimeFormatter);
       //获取七天前的时间
        LocalDateTime goal = thisDay.minusDays(7);
        //进行查询
        QueryWrapper<SearchHotWords> wrapper = new QueryWrapper<>();
        wrapper.gt("create_time",goal).orderByDesc("score").eq("isdel",0);//大于七天前的这个时间
        List<SearchHotWords> list = hotWordsMapper.selectList(wrapper);
        return list;
    }

    @Override
    public int deleteWords(long id) {
        int rs = hotWordsMapper.deleteHotWords(id);
        return rs;
    }
}
